import numpy as np
import operator as op

# 伪造一个数据集
group=np.array([[1.0,1.1],[1.0,1.0],[0.0,0.0],[0.0,0.1]])
labels=['A','A','B','B']

# knn模型部分
dataSetSize=group.shape[0]

# 计算距离
diffMat=np.tile([0.0,0.0],(dataSetSize,1))-group
sqDiffMat=diffMat**2
sqDistances=sqDiffMat.sum(axis=1)
distances=sqDistances**0.5

sortedDistIndicies=distances.argsort()
classCount={}
for i in range(3):
    voteIlabel=labels[sortedDistIndicies[i]]
    # 选择距离最小的K点
    classCount[voteIlabel]=classCount.get(voteIlabel,0)+1

# 排序
sortedClassCount=sorted(classCount.items(),key=op.itemgetter(1),reverse=True)

print(sortedClassCount)